﻿using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.IO;

public static class TexAttributeEx
{
	public static bool enbleSetSpriteSheet = true;
	public static int MAX_SIZE = 2048;
	public static void setEvonyAttributes(this TextureImporter textureImporter)
	{
		#if UNITY_5_5_OR_NEWER
		if(textureImporter.textureType != TextureImporterType.Sprite)
		{
			textureImporter.textureType = TextureImporterType.Sprite;
		}
		#else
		textureImporter.textureType = TextureImporterType.Advanced;
		#endif


		textureImporter.mipmapEnabled = false;
		textureImporter.maxTextureSize = MAX_SIZE;
        textureImporter.alphaIsTransparency = true;
		textureImporter.wrapMode = UnityEngine.TextureWrapMode.Clamp;
		TextureImporterSettings s = new TextureImporterSettings();
		textureImporter.ReadTextureSettings(s);
		s.spriteMeshType = SpriteMeshType.FullRect;

		if(s.spriteMode == (int)SpriteImportMode.None)
		{
			s.spriteMode = (int)SpriteImportMode.Single;
		}
		
		if(textureImporter.spriteImportMode == SpriteImportMode.None)
		{
			textureImporter.spriteImportMode = SpriteImportMode.Single;
		}

		#if UNITY_5_5_OR_NEWER
		if(textureImporter.assetPath.Contains("/" + ResourcesEx.SheetDirName + "/"))
		{
			textureImporter.spriteImportMode = SpriteImportMode.Multiple;
		}
		#endif

		textureImporter.SetTextureSettings(s);
		
		
		


		if(!File.Exists("/usr/local/bin/TexturePacker"))
		{
			return;
		}
		
		if(!enbleSetSpriteSheet)
		{
			return;
		}
		
		
		
		BuildTarget target = EditorCommonTool.build_target_ios;
		int index = textureImporter.assetPath.IndexOf(EditorCommonTool.getSheetDirName(EditorCommonTool.build_target_ios));
		if(index < 0)
		{
			index = textureImporter.assetPath.IndexOf(EditorCommonTool.getSheetDirName(BuildTarget.Android));
			if(index >= 0)
			{
				target = BuildTarget.Android;
			}
		}
		
		string resPath = null;
		if(index >= 0)
		{
			resPath = textureImporter.assetPath.Substring(0, index - 1);
			
			SpriteSheetConfig config = EditorCommonTool.getSpriteSheetConfig(resPath, target.getPlatform());
			string spriteSheetName = MaterialTextureForETC1.getSpriteSheetNameBySpriteSheetPath(textureImporter.assetPath);
			SpriteSheetInfo info = null;
			config.TryGetValue(spriteSheetName, out info);
			if(info != null)
			{
				textureImporter.spritePixelsPerUnit = 100 * info.getScale();
			}
			


			if(target == EditorCommonTool.build_target_ios)
			{
                int oldMaxSize = 0;
                TextureImporterFormat format = TextureImporterFormat.Alpha8;
                int compressQuality = 0;
                if (!SetPlatformTextureSettings(textureImporter, "iPhone", out oldMaxSize, out format, out compressQuality))
                {
                    oldMaxSize = MAX_SIZE;
                }

				if(!textureImporter.DoesSourceTextureHaveAlpha())
				{
                    SetPlatformTextureSettings(textureImporter, "iPhone", Mathf.Min(oldMaxSize, MAX_SIZE), TextureImporterFormat.PVRTC_RGB4, 100);
				}
				else
				{
                    SetPlatformTextureSettings(textureImporter, "iPhone", Mathf.Min(oldMaxSize, MAX_SIZE), TextureImporterFormat.PVRTC_RGBA4, 100);
				}
			}
			else if(textureImporter.assetPath.Contains(EditorCommonTool.getSheetDirName(BuildTarget.Android)))
			{
                int oldMaxSize = 0;
                TextureImporterFormat format = TextureImporterFormat.Alpha8;
                int compressQuality = 0;
                if (!SetPlatformTextureSettings(textureImporter, "Android", out oldMaxSize, out format, out compressQuality))
                {
                    oldMaxSize = MAX_SIZE;
                }

				if(!textureImporter.DoesSourceTextureHaveAlpha())
				{
                    SetPlatformTextureSettings(textureImporter, "Android", Mathf.Min(oldMaxSize, MAX_SIZE), TextureImporterFormat.ETC_RGB4, 0);
				}
				else
				{
                    SetPlatformTextureSettings(textureImporter, "Android", Mathf.Min(oldMaxSize, MAX_SIZE), TextureImporterFormat.ETC2_RGBA8, 0);
				}
			}
		}
	}
	
	public static void SetPlatformTextureSettings(TextureImporter importer, string platform, int maxTextureSize, TextureImporterFormat textureFormat, int compressionQuality)
	{
        #if UNITY_5_3_OR_NEWER
		importer.SetPlatformTextureSettings(platform, maxTextureSize, textureFormat, compressionQuality, false);
		#else
		importer.SetPlatformTextureSettings(platform, maxTextureSize, textureFormat, compressionQuality);
		#endif
	}

    public static bool SetPlatformTextureSettings(TextureImporter importer, string platform, out int maxTextureSize, out TextureImporterFormat textureFormat, out int compressionQuality)
    {
        #if UNITY_5_3_OR_NEWER
        bool compress = false;
        return importer.GetPlatformTextureSettings(platform, out maxTextureSize, out textureFormat, out compressionQuality, out compress);
        #else
        return importer.GetPlatformTextureSettings(platform, out maxTextureSize, out textureFormat, out compressionQuality);
        #endif
    }
}

